package com.emapp.lccode.activity

import android.content.Intent
import android.os.Bundle
import com.emapp.lccode.R
import com.emapp.lccode.adapter.ProgressImageAdapter
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.bean.DeviceKt
import com.emapp.lccode.bean.OrderFileKt
import com.emapp.lccode.bean.Project
import com.emapp.lccode.databinding.ActivityProjectInfoBinding
import com.emapp.lccode.mode.ProjectViewModel
import com.emapp.lccode.utils.DividerOrientation
import com.emapp.lccode.utils.GlideEngine
import com.emapp.lccode.utils.MmkvUtil
import com.emapp.lccode.utils.divider
import com.emapp.lccode.utils.getColorExt
import com.emapp.lccode.utils.grid
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.emapp.lccode.utils.toEntity
import com.gyf.immersionbar.ktx.immersionBar
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.style.PictureSelectorStyle
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 项目概况
 */
class ProjectInfoActivity : BaseActivity1<ProjectViewModel, ActivityProjectInfoBinding>() {
    private val imageAdapter: ProgressImageAdapter by lazy { ProgressImageAdapter() }
    var images: ArrayList<LocalMedia> = arrayListOf()
    lateinit var infor: Project
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
            fullScreen(true)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.white)
        }
       // infor = intent.getSerializableExtra("infor") as Project? ?: Project()
        val inf = MmkvUtil.getString("infor","")
        if (inf.isNotEmpty()){
            infor= inf.toEntity<Project>()!!
            MmkvUtil.putString("infor","")
        }
        mViewBind.lvTitle.tvTitle.text = "项目概况"
        // mViewBind.lvTitle.tvRight.text = "项目进度"
        EventBus.getDefault().register(this)
        mViewBind.rvList.run {
            grid(4)
            adapter = imageAdapter
            divider {
                orientation = DividerOrientation.HORIZONTAL
                includeVisible = true
                setDivider(10, true)
                setColor(getColorExt(R.color.white))
            }
        }
        imageAdapter.setOnItemClickListener { adapter, _, position ->
            PictureSelector.create(this)
                .openPreview()
                .setImageEngine(GlideEngine.createGlideEngine())
                .setSelectorUIStyle(PictureSelectorStyle())
                .isPreviewFullScreenMode(true)
                .startActivityPreview(position, false, images)
        }
        setInfo()
    }

    override fun createObserver() {
        mViewModel.run {
        }
    }

    fun setInfo() {
        mViewBind.run {
            tvName.text = infor.name
            tvDate.text = infor.year
            tvControlCompany.text = infor.control_company
            tvBuildCompany.text = infor.build_company
            tvContent.text = infor.content
        }
        val fs: ArrayList<OrderFileKt> = arrayListOf()
        fs.add(OrderFileKt("", "", "", infor.thumb))
        val l = LocalMedia()
        l.path = BaseConfig.ROOT_URL + infor.thumb
        images.add(l)
        imageAdapter.submitList(fs)
//        if (infor.fileArray != null) {
//            images.clear()
//            info.fileArray.forEach {
//                var l = LocalMedia()
//                l.path = BaseConfig.ROOT_URL + it.filepath
//                images.add(l)
//            }
//            imageAdapter.submitList(info.fileArray)
//        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mViewBind.lvTitle.ivLeft,
            mViewBind.lvTitle.tvRight, mViewBind.tvProgress
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }

                R.id.tv_right -> {
                }

                R.id.tv_progress -> {
                    val intent = Intent(this, ProjectProgressListActivity::class.java)
                    intent.putExtra("project_id", infor.id)
                    intent.putExtra("project_name", infor.sort_name)
                    startActivity(intent)
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.PROJECT_DEVICE -> {
                // infor=event.`object` as DeviceKt
            }

            else -> {}
        }
    }

}
